﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using insurance.application;
using System.Collections;

public partial class product_insurance_user_insurance_apply_hospital : System.Web.UI.Page
{
    private const String FILE_PATH = "~/upload/insurance/";

    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "ACCOUNTS";
        if (!IsPostBack)
        {
            this.btnSubmit.Enabled = false;

            this.tb_firstName.Text = Session["fName"] as String;
            this.tb_lastName.Text = Session["lName"] as String;
            this.tb_nric.Text = Session["custNRIC"] as String;
            this.tb_nationality.Text = Session["custNationality"] as String;
            this.ddl_maritalStatus.SelectedValue = Session["custMarital"] as String;
            String gender = Session["custGender"] as String;
            if ("Female".Equals(gender))
            {
                this.rb_female.Checked = true;
            }
            else
            {

            } this.rb_male.Checked = true;

            Object dob = Session["custDob"];
            if (dob != null)
            {
                this.tb_dob.Text = ((DateTime)Session["custDob"]).ToString("dd/MM/yyyy");
            }

            this.tb_tel.Text = Session["custTel"] as String;
            this.tb_mobile.Text = Session["custMobile"] as String;
            this.tb_email.Text = Session["custEmail"] as String;
            this.tb_mailingAddress.Text = Session["custAddress"] as String;
            this.tb_postal.Text = Session["custPostal"] as String;
            this.tb_occupation.Text = Session["custOccupation"] as String;

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (!Page.IsValid) return;

        var applicationEntity = new ApplyMainBLL{
            CustId = (String) Session["userId"],
            ApplicationSatus = "PENDING",
            AccountNo = "",
            ApplicationDate = DateTime.Now,
            StartDate = Util.StringToDate(tbStartDate.Text),
            EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
            PaymentWay = ddlPaymentMethod.SelectedValue,
            StaffId = null,
            SavingAccountNo = (String) Session["savingAccountNo"],
            ApplyCategory = "",
            ApplyProduct = "INSURANCE",
            ProductType = "H"
        };


        // applicationEntity.ApprovalDate=null;
        // Console.WriteLine(applicationEntity.ToString());

        var insureds = new LinkedList<ApplyInsuredBLL>();

        if (!Util.IsInputBlank(tb_firstName.Text))
        {
            var selfInsuranedEntity = new ApplyInsuredBLL
            {
                FirstName = tb_firstName.Text,
                LastName = tb_lastName.Text,
                Nric = tb_nric.Text,
                Nationality = tb_nationality.Text,
                IsMale = rb_male.Checked,
                MaritalStatus = ddl_maritalStatus.Text,
                Dob = Util.StringToDate(tb_dob.Text),
                Occupation = tb_occupation.Text,
                Relationship = "SELF",
                Tel = tb_tel.Text,
                Mobile = tb_mobile.Text,
                Email = tb_email.Text,
                MailingAddress = tb_mailingAddress.Text,
                Postal = tb_postal.Text,
                Height = null,
                Weight = null,
                IsSmoker = null,
                Employer = tbe_name.Text,
                CompanyAddress = tbe_address.Text,
                Business = tbe_business.Text,
                StartDate = Util.StringToDate(this.tbStartDate.Text),
                EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
                Plan = ddl_plan.Text,

                DeclinedHistory = (String)(GetDeclinedHistory(tb_firstName.Text + " " + tb_lastName.Text)["DeclinedHistory"]),
                ForeseeableIllness = (String)(GetForeseeableIllness(tb_firstName.Text + " " + tb_lastName.Text)["ForeseeableIllness"]),                
                                
                NatureOfIllness = (String)(GetNatureTblValue(tb_firstName.Text + " " + tb_lastName.Text)["NatureOfIllness"]),
                NatureDate = (String)(GetNatureTblValue(tb_firstName.Text + " " + tb_lastName.Text)["NatureDate"]),
                ResultOfTreatment = (String)(GetNatureTblValue(tb_firstName.Text + " " + tb_lastName.Text)["ResultOfTreatment"]),
                NatureHospital = (String)(GetNatureTblValue(tb_firstName.Text + " " + tb_lastName.Text)["NatureHospital"]),

                HyperlipidaemiaReading = (String)(GetHyperlipTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HyperlipidaemiaReading"]),
                HyperlipidaemiaDate = (String)(GetHyperlipTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HyperlipidaemiaDate"]),
                HyperlipidaemiaHospital = (String)(GetHyperlipTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HyperlipidaemiaHospital"]),

                HighCholesterolLevel = (String)(GetHighCholeTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HighCholesterolLevel"]),
                HighCholesterolDate = (String)(GetHighCholeTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HighCholesterolDate"]),
                HighCholesterolHospital = (String)(GetHighCholeTblValue(tb_firstName.Text + " " + tb_lastName.Text)["HighCholesterolHospital"])
                
            };


            selfInsuranedEntity.docPath = FILE_PATH + fileUpload(fu_doc);
          
            insureds.AddLast(selfInsuranedEntity);
        }



        if (!Util.IsInputBlank(tbs_fname.Text))
        {
            var spouseInsuranedEntity = new ApplyInsuredBLL
                {

                    FirstName = tbs_fname.Text,
                    LastName = tbs_lname.Text,
                    Nric = tb_nric.Text,
                    Nationality = null,
                    IsMale = rbs_male.Checked,
                    MaritalStatus = "Married",
                    Dob = Util.StringToDate(tbs_dob.Text),
                    Occupation = null,
                    Relationship = "SPOUSE",
                    Tel = null,
                    Mobile = null,
                    Email = null,
                    MailingAddress = null,
                    Postal = null,
                    Height = tbs_height.Text != null ? Convert.ToDecimal(tbs_height.Text) : 0,
                    Weight = tbs_weight.Text != null ? Convert.ToDecimal(tbs_weight.Text) : 0,
                    IsSmoker = rbs_smoke.Checked,
                    Employer = null,
                    CompanyAddress = null,
                    Business = null,
                    StartDate = Util.StringToDate(this.tbStartDate.Text),
                    EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
                    
                    Plan = ddl_sPlan.Text,

                    DeclinedHistory = (String)(GetDeclinedHistory(tbs_fname.Text + " " + tbs_lname.Text)["DeclinedHistory"]),
                    ForeseeableIllness = (String)(GetForeseeableIllness(tbs_fname.Text + " " + tbs_lname.Text)["ForeseeableIllness"]),

                    NatureOfIllness = (String)(GetNatureTblValue(tbs_fname.Text + " " + tbs_lname.Text)["NatureOfIllness"]),
                    NatureDate = (String)(GetNatureTblValue(tbs_fname.Text + " " + tbs_lname.Text)["NatureDate"]),
                    ResultOfTreatment = (String)(GetNatureTblValue(tbs_fname.Text + " " + tbs_lname.Text)["ResultOfTreatment"]),
                    NatureHospital = (String)(GetNatureTblValue(tbs_fname.Text + " " + tbs_lname.Text)["NatureHospital"]),

                    HyperlipidaemiaReading = (String)(GetHyperlipTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HyperlipidaemiaReading"]),
                    HyperlipidaemiaDate = (String)(GetHyperlipTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HyperlipidaemiaDate"]),
                    HyperlipidaemiaHospital = (String)(GetHyperlipTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HyperlipidaemiaHospital"]),

                    HighCholesterolLevel = (String)(GetHighCholeTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HighCholesterolLevel"]),
                    HighCholesterolDate = (String)(GetHighCholeTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HighCholesterolDate"]),
                    HighCholesterolHospital = (String)(GetHighCholeTblValue(tbs_fname.Text + " " + tbs_lname.Text)["HighCholesterolHospital"])
                  
           
                };


            insureds.AddLast(spouseInsuranedEntity);

        }


        if (!Util.IsInputBlank(tbc1_fname.Text))
        {

            var child1InsuranedEntity = new ApplyInsuredBLL
                {

                    FirstName = tbc1_fname.Text,
                    LastName = tbc1_lname.Text,
                    Nric = tbc1_nric.Text,
                    Nationality = null,
                    IsMale = rbc1_male.Checked,
                    MaritalStatus = "Single",
                    Dob = Util.StringToDate(tbc1_dob.Text),
                    Occupation = null,
                    Relationship = "CHILD",
                    Tel = null,
                    Mobile = null,
                    Email = null,
                    MailingAddress = null,
                    Postal = null,
                    Height = tbs_height.Text != null ? Convert.ToDecimal(tbc1_height.Text) : 0,
                    Weight = tbs_weight.Text != null ? Convert.ToDecimal(tbc1_weight.Text) : 0,
                    IsSmoker = rbc1_smoke.Checked,
                    Employer = null,
                    CompanyAddress = null,
                    Business = null,
                    StartDate = Util.StringToDate(this.tbStartDate.Text),
                    EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
                    
                    Plan = ddl_c1Plan.Text,


                    DeclinedHistory = (String)(GetDeclinedHistory(tbc1_fname.Text + " " + tbc1_lname.Text)["DeclinedHistory"]),
                    ForeseeableIllness = (String)(GetForeseeableIllness(tbc1_fname.Text + " " + tbc1_lname.Text)["ForeseeableIllness"]),

                    NatureOfIllness = (String)(GetNatureTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["NatureOfIllness"]),
                    NatureDate = (String)(GetNatureTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["NatureDate"]),
                    ResultOfTreatment = (String)(GetNatureTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["ResultOfTreatment"]),
                    NatureHospital = (String)(GetNatureTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["NatureHospital"]),

                    HyperlipidaemiaReading = (String)(GetHyperlipTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HyperlipidaemiaReading"]),
                    HyperlipidaemiaDate = (String)(GetHyperlipTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HyperlipidaemiaDate"]),
                    HyperlipidaemiaHospital = (String)(GetHyperlipTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HyperlipidaemiaHospital"]),

                    HighCholesterolLevel = (String)(GetHighCholeTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HighCholesterolLevel"]),
                    HighCholesterolDate = (String)(GetHighCholeTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HighCholesterolDate"]),
                    HighCholesterolHospital = (String)(GetHighCholeTblValue(tbc1_fname.Text + " " + tbc1_lname.Text)["HighCholesterolHospital"])
                  
           
                };

            insureds.AddLast(child1InsuranedEntity);

        }


        if (!Util.IsInputBlank(tbc2_fname.Text))
        {

            var child2InsuranedEntity = new ApplyInsuredBLL
            {

                FirstName = tbc2_fname.Text,
                LastName = tbc2_lname.Text,
                Nric = tbc2_nric.Text,
                Nationality = null,
                IsMale = rbc2_male.Checked,
                MaritalStatus = "Single",
                Dob = Util.StringToDate(tbc2_dob.Text),
                Occupation = null,
                Relationship = "CHILD",
                Tel = null,
                Mobile = null,
                Email = null,
                MailingAddress = null,
                Postal = null,
                Height = tbs_height.Text != null ? Convert.ToDecimal(tbc2_height.Text) : 0,
                Weight = tbs_weight.Text != null ? Convert.ToDecimal(tbc2_weight.Text) : 0,
                IsSmoker = rbc2_smoke.Checked,
                Employer = null,
                CompanyAddress = null,
                Business = null,
                StartDate = Util.StringToDate(this.tbStartDate.Text),
                EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
               
                Plan = ddl_c2Plan.Text,

                DeclinedHistory = (String)(GetDeclinedHistory(tbc2_fname.Text + " " + tbc2_lname.Text)["DeclinedHistory"]),
                ForeseeableIllness = (String)(GetForeseeableIllness(tbc2_fname.Text + " " + tbc2_lname.Text)["ForeseeableIllness"]),                

                NatureOfIllness = (String)(GetNatureTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["NatureOfIllness"]),
                NatureDate = (String)(GetNatureTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["NatureDate"]),
                ResultOfTreatment = (String)(GetNatureTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["ResultOfTreatment"]),
                NatureHospital = (String)(GetNatureTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["NatureHospital"]),

                HyperlipidaemiaReading = (String)(GetHyperlipTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HyperlipidaemiaReading"]),
                HyperlipidaemiaDate = (String)(GetHyperlipTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HyperlipidaemiaDate"]),
                HyperlipidaemiaHospital = (String)(GetHyperlipTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HyperlipidaemiaHospital"]),

                HighCholesterolLevel = (String)(GetHighCholeTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HighCholesterolLevel"]),
                HighCholesterolDate = (String)(GetHighCholeTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HighCholesterolDate"]),
                HighCholesterolHospital = (String)(GetHighCholeTblValue(tbc2_fname.Text + " " + tbc2_lname.Text)["HighCholesterolHospital"])


            };

            insureds.AddLast(child2InsuranedEntity);

        }


        if (!Util.IsInputBlank(tbc3_fname.Text))
        {

            var child3InsuranedEntity = new ApplyInsuredBLL
                {

                    FirstName = tbc3_fname.Text,
                    LastName = tbc3_lname.Text,
                    Nric = tbc3_nric.Text,
                    Nationality = null,
                    IsMale = rbc3_male.Checked,
                    MaritalStatus = "Single",
                    Dob = Util.StringToDate(tbc3_dob.Text),
                    Occupation = null,
                    Relationship = "CHILD",
                    Tel = null,
                    Mobile = null,
                    Email = null,
                    MailingAddress = null,
                    Postal = null,
                    Height = tbs_height.Text != null ? Convert.ToDecimal(tbc3_height.Text) : 0,
                    Weight = tbs_weight.Text != null ? Convert.ToDecimal(tbc3_weight.Text) : 0,
                    IsSmoker = rbc3_smoke.Checked,
                    Employer = null,
                    CompanyAddress = null,
                    Business = null,
                    StartDate = Util.StringToDate(this.tbStartDate.Text),
                    EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
                   
                    Plan = ddl_c3Plan.Text,

                    DeclinedHistory = (String)(GetDeclinedHistory(tbc3_fname.Text + " " + tbc3_lname.Text)["DeclinedHistory"]),
                    ForeseeableIllness = (String)(GetForeseeableIllness(tbc3_fname.Text + " " + tbc3_lname.Text)["ForeseeableIllness"]),                

                    NatureOfIllness = (String)(GetNatureTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["NatureOfIllness"]),
                    NatureDate = (String)(GetNatureTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["NatureDate"]),
                    ResultOfTreatment = (String)(GetNatureTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["ResultOfTreatment"]),
                    NatureHospital = (String)(GetNatureTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["NatureHospital"]),

                    HyperlipidaemiaReading = (String)(GetHyperlipTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HyperlipidaemiaReading"]),
                    HyperlipidaemiaDate = (String)(GetHyperlipTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HyperlipidaemiaDate"]),
                    HyperlipidaemiaHospital = (String)(GetHyperlipTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HyperlipidaemiaHospital"]),

                    HighCholesterolLevel = (String)(GetHighCholeTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HighCholesterolLevel"]),
                    HighCholesterolDate = (String)(GetHighCholeTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HighCholesterolDate"]),
                    HighCholesterolHospital = (String)(GetHighCholeTblValue(tbc3_fname.Text + " " + tbc3_lname.Text)["HighCholesterolHospital"])
                  
           
                };

            insureds.AddLast(child3InsuranedEntity);

        }

        
        applicationEntity.Insureds = insureds;

        // INSERT APPLICATION_INSURANCE AND APPLICATION_INSURANCE_INSURED TABLE
        applicationEntity.ApplyInsurance();

        this.Response.Redirect("./insurance_after_submit.aspx");
    }



    private Hashtable GetNatureTblValue(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tb_nature1.Text))
        {
            if (tb_name1.Text == fullName)
            {
                hash.Add("NatureOfIllness", tb_nature1.Text);
                hash.Add("NatureDate", tb_duration1.Text);
                hash.Add("ResultOfTreatment", tb_result1.Text);
                hash.Add("NatureHospital", tb_address1.Text);
            }
        }
        else if (!Util.IsInputBlank(tb_nature2.Text))
        {
            if (tb_name2.Text == fullName)
            {
                hash.Add("NatureOfIllness", tb_nature2.Text);
                hash.Add("NatureDate", tb_duration2.Text);
                hash.Add("ResultOfTreatment", tb_result2.Text);
                hash.Add("NatureHospital", tb_address2.Text);
            }
        }
        else if (!Util.IsInputBlank(tb_nature3.Text))
        {
            if (tb_name3.Text == fullName)
            {
                hash.Add("NatureOfIllness", tb_nature3.Text);
                hash.Add("NatureDate", tb_duration3.Text);
                hash.Add("ResultOfTreatment", tb_result3.Text);
                hash.Add("NatureHospital", tb_address3.Text);
            }
        }
        else if (!Util.IsInputBlank(tb_nature3.Text))
        {
            if (tb_name4.Text == fullName)
            {
                hash.Add("NatureOfIllness", tb_nature4.Text);
                hash.Add("NatureDate", tb_duration4.Text);
                hash.Add("ResultOfTreatment", tb_result4.Text);
                hash.Add("NatureHospital", tb_address4.Text);
            }
        }

        return hash;
    }


    private Hashtable GetHyperlipTblValue(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbhbp_name1.Text))
        {
            if (tbhbp_name1.Text == fullName)
            {
                hash.Add("HyperlipidaemiaReading", tbhbp_reading1.Text);
                hash.Add("HyperlipidaemiaDate", tbhbp_date1.Text);
                hash.Add("HyperlipidaemiaHospital", tbhbp_address1.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhbp_name2.Text))
        {
            if (tbhbp_name2.Text == fullName)
            {
                hash.Add("HyperlipidaemiaReading", tbhbp_reading2.Text);
                hash.Add("HyperlipidaemiaDate", tbhbp_date2.Text);
                hash.Add("HyperlipidaemiaHospital", tbhbp_address2.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhbp_name3.Text))
        {
            if (tbhbp_name3.Text == fullName)
            {
                hash.Add("HyperlipidaemiaReading", tbhbp_reading3.Text);
                hash.Add("HyperlipidaemiaDate", tbhbp_date3.Text);
                hash.Add("HyperlipidaemiaHospital", tbhbp_address3.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhbp_name4.Text))
        {
            if (tbhbp_name4.Text == fullName)
            {
                hash.Add("HyperlipidaemiaReading", tbhbp_reading4.Text);
                hash.Add("HyperlipidaemiaDate", tbhbp_date4.Text);
                hash.Add("HyperlipidaemiaHospital", tbhbp_address4.Text);
            }
        }

        return hash;
    }

    private Hashtable GetHighCholeTblValue(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbhc_name1.Text))
        {
            if (tbhc_name1.Text == fullName)
            {
                hash.Add("HighCholesterolLevel", tbhc_level1.Text);
                hash.Add("HighCholesterolDate", tbhc_date1.Text);
                hash.Add("HighCholesterolHospital", tbhc_address1.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhc_name2.Text))
        {
            if (tbhc_name2.Text == fullName)
            {
                hash.Add("HighCholesterolLevel", tbhc_level2.Text);
                hash.Add("HighCholesterolDate", tbhc_date2.Text);
                hash.Add("HighCholesterolHospital", tbhc_address2.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhc_name3.Text))
        {
            if (tbhc_name3.Text == fullName)
            {
                hash.Add("HighCholesterolLevel", tbhc_level3.Text);
                hash.Add("HighCholesterolDate", tbhc_date3.Text);
                hash.Add("HighCholesterolHospital", tbhc_address3.Text);

            }
        }
        else if (!Util.IsInputBlank(tbhc_name4.Text))
        {
            if (tbhc_name4.Text == fullName)
            {
                hash.Add("HighCholesterolLevel", tbhc_level4.Text);
                hash.Add("HighCholesterolDate", tbhc_date4.Text);
                hash.Add("HighCholesterolHospital", tbhc_address4.Text);
            }
        }

        return hash;
    }

    private Hashtable GetDeclinedHistory(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq3_name1.Text))
        {
            if (tbq3_name1.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq3_declinedHistory1.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq3_name2.Text))
        {
            if (tbq3_name2.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq3_declinedHistory2.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq3_name3.Text))
        {
            if (tbq3_name3.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq3_declinedHistory3.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq3_name4.Text))
        {
            if (tbq3_name4.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq3_declinedHistory4.Text);
            }
        }

        return hash;
    }

    private Hashtable GetForeseeableIllness(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq4_name1.Text))
        {
            if (tbq4_name1.Text == fullName)
            {
                hash.Add("ForeseeableIllness", tbq4_foreseeableIllness1.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq4_name2.Text))
        {
            if (tbq4_name2.Text == fullName)
            {
                hash.Add("ForeseeableIllness", tbq4_foreseeableIllness2.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq4_name3.Text))
        {
            if (tbq4_name3.Text == fullName)
            {
                hash.Add("ForeseeableIllness", tbq4_foreseeableIllness3.Text);

            }
        }
        else if (!Util.IsInputBlank(tbq4_name4.Text))
        {
            if (tbq4_name4.Text == fullName)
            {
                hash.Add("ForeseeableIllness", tbq4_foreseeableIllness4.Text);
            }
        }

        return hash;
    }

    /// <summary>
    /// Check Family input
    /// </summary>
    /// <param name="source"></param>
    /// <param name="arguments"></param>
    public void vd_family_ServerValidation(object source, ServerValidateEventArgs arguments)
    {

        // Check Spouse
        if (!Util.IsInputBlank(this.tbs_fname.Text) ||
            !Util.IsInputBlank(this.tbs_lname.Text) ||
            !Util.IsInputBlank(this.tbs_nric.Text) ||
            !Util.IsInputBlank(this.tbs_dob.Text) ||
            (rbs_male.Checked || rbs_female.Checked) ||
            !Util.IsInputBlank(this.tbs_weight.Text) ||
            (rbs_smoke.Checked||rbs_nsmoke.Checked))
        {

            if (!Util.IsInputBlank(this.tbs_fname.Text) &&
           !Util.IsInputBlank(this.tbs_lname.Text) &&
           !Util.IsInputBlank(this.tbs_nric.Text) &&
           !Util.IsInputBlank(this.tbs_dob.Text) &&
           (rbs_male.Checked || rbs_female.Checked) &&
           !Util.IsInputBlank(this.tbs_weight.Text) &&
           !Util.IsInputBlank(this.tbs_height.Text) &&
           (rbs_smoke.Checked || rbs_nsmoke.Checked))
            {
                arguments.IsValid = true;
            }
            else
            {
                arguments.IsValid = false;
            }
        }


        // Check Child1
        if (!Util.IsInputBlank(this.tbc1_fname.Text) ||
           !Util.IsInputBlank(this.tbc1_lname.Text) ||
           !Util.IsInputBlank(this.tbc1_nric.Text) ||
           !Util.IsInputBlank(this.tbc1_dob.Text) ||
           (rbc1_male.Checked || rbc1_female.Checked) ||
           !Util.IsInputBlank(this.tbc1_weight.Text) ||
           !Util.IsInputBlank(this.tbc1_height.Text) ||
           (rbc1_smoke.Checked || rbc1_nsmoke.Checked))
        {

            if (!Util.IsInputBlank(this.tbc1_fname.Text) &&
           !Util.IsInputBlank(this.tbc1_lname.Text) &&
           !Util.IsInputBlank(this.tbc1_nric.Text) &&
           !Util.IsInputBlank(this.tbc1_dob.Text) &&
           (rbc1_male.Checked || rbc1_female.Checked) &&
           !Util.IsInputBlank(this.tbc1_weight.Text) &&
           !Util.IsInputBlank(this.tbc1_height.Text) &&
           (rbc1_smoke.Checked || rbc1_nsmoke.Checked))
            {
                arguments.IsValid = true;
            }
            else
            {
                arguments.IsValid = false;
            }
        }


        // Check Child2
        if (!Util.IsInputBlank(this.tbc2_fname.Text) ||
           !Util.IsInputBlank(this.tbc2_lname.Text) ||
           !Util.IsInputBlank(this.tbc2_nric.Text) ||
           !Util.IsInputBlank(this.tbc2_dob.Text) ||
           (rbc2_male.Checked || rbc2_female.Checked) ||
           !Util.IsInputBlank(this.tbc2_weight.Text) ||
           !Util.IsInputBlank(this.tbc2_height.Text) ||
           (rbc1_smoke.Checked || rbc2_nsmoke.Checked))
        {

            if (!Util.IsInputBlank(this.tbc2_fname.Text) &&
           !Util.IsInputBlank(this.tbc2_lname.Text) &&
           !Util.IsInputBlank(this.tbc2_nric.Text) &&
           !Util.IsInputBlank(this.tbc2_dob.Text) &&
           (rbc2_male.Checked || rbc2_female.Checked) &&
           !Util.IsInputBlank(this.tbc2_weight.Text) &&
           !Util.IsInputBlank(this.tbc2_height.Text) &&
           (rbc2_smoke.Checked || rbc2_nsmoke.Checked))
            {
                arguments.IsValid = true;
            }
            else
            {
                arguments.IsValid = false;
            }
        }


        // Check Child3
        if (!Util.IsInputBlank(this.tbc3_fname.Text) ||
           !Util.IsInputBlank(this.tbc3_lname.Text) ||
           !Util.IsInputBlank(this.tbc3_nric.Text) ||
           !Util.IsInputBlank(this.tbc3_dob.Text) ||
           (rbc2_male.Checked || rbc3_female.Checked) ||
           !Util.IsInputBlank(this.tbc3_weight.Text) ||
           !Util.IsInputBlank(this.tbc3_height.Text) ||
           (rbc1_smoke.Checked || rbc3_nsmoke.Checked))
        {

            if (!Util.IsInputBlank(this.tbc3_fname.Text) &&
           !Util.IsInputBlank(this.tbc3_lname.Text) &&
           !Util.IsInputBlank(this.tbc3_nric.Text) &&
           !Util.IsInputBlank(this.tbc3_dob.Text) &&
           (rbc3_male.Checked || rbc3_female.Checked) &&
           !Util.IsInputBlank(this.tbc3_weight.Text) &&
           !Util.IsInputBlank(this.tbc3_height.Text) &&
           (rbc3_smoke.Checked || rbc3_nsmoke.Checked))
            {
                arguments.IsValid = true;
            }
            else
            {
                arguments.IsValid = false;
            }
        }
   
    }

    /// <summary>
    /// Check illness input
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void vd_illness_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        if (!Util.IsInputBlank(this.tb_name1.Text) ||
            !Util.IsInputBlank(this.tb_nature1.Text) ||
            !Util.IsInputBlank(this.tb_duration1.Text) ||
            !Util.IsInputBlank(this.tb_result1.Text) ||
            !Util.IsInputBlank(this.tb_address1.Text))
        {

            if (!Util.IsInputBlank(this.tb_name1.Text) &&
           !Util.IsInputBlank(this.tb_nature1.Text) &&
           !Util.IsInputBlank(this.tb_duration1.Text) &&
           !Util.IsInputBlank(this.tb_result1.Text) &&
           !Util.IsInputBlank(this.tb_address1.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 2
        if (!Util.IsInputBlank(this.tb_name2.Text) ||
            !Util.IsInputBlank(this.tb_nature2.Text) ||
            !Util.IsInputBlank(this.tb_duration2.Text) ||
            !Util.IsInputBlank(this.tb_result2.Text) ||
            !Util.IsInputBlank(this.tb_address2.Text))
        {

            if (!Util.IsInputBlank(this.tb_name2.Text) &&
           !Util.IsInputBlank(this.tb_nature2.Text) &&
           !Util.IsInputBlank(this.tb_duration2.Text) &&
           !Util.IsInputBlank(this.tb_result2.Text) &&
           !Util.IsInputBlank(this.tb_address2.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 3
        if (!Util.IsInputBlank(this.tb_name3.Text) ||
            !Util.IsInputBlank(this.tb_nature3.Text) ||
            !Util.IsInputBlank(this.tb_duration3.Text) ||
            !Util.IsInputBlank(this.tb_result3.Text) ||
            !Util.IsInputBlank(this.tb_address3.Text))
        {

            if (!Util.IsInputBlank(this.tb_name4.Text) &&
           !Util.IsInputBlank(this.tb_nature4.Text) &&
           !Util.IsInputBlank(this.tb_duration4.Text) &&
           !Util.IsInputBlank(this.tb_result4.Text) &&
           !Util.IsInputBlank(this.tb_address4.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }


        // Check item 4
        if (!Util.IsInputBlank(this.tb_name4.Text) ||
            !Util.IsInputBlank(this.tb_nature4.Text) ||
            !Util.IsInputBlank(this.tb_duration4.Text) ||
            !Util.IsInputBlank(this.tb_result4.Text) ||
            !Util.IsInputBlank(this.tb_address4.Text))
        {

            if (!Util.IsInputBlank(this.tb_name4.Text) &&
           !Util.IsInputBlank(this.tb_nature4.Text) &&
           !Util.IsInputBlank(this.tb_duration4.Text) &&
           !Util.IsInputBlank(this.tb_result4.Text) &&
           !Util.IsInputBlank(this.tb_address4.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

    }
    protected void vd_declinedHistory_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        if (!Util.IsInputBlank(this.tbq3_name1.Text) ||
            !Util.IsInputBlank(this.tbq3_declinedHistory1.Text))
        {

            if (!Util.IsInputBlank(this.tbq3_name1.Text) &&
            !Util.IsInputBlank(this.tbq3_declinedHistory1.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 2
        if (!Util.IsInputBlank(this.tbq3_name2.Text) ||
            !Util.IsInputBlank(this.tbq3_declinedHistory2.Text))
        {

            if (!Util.IsInputBlank(this.tbq3_name2.Text) &&
            !Util.IsInputBlank(this.tbq3_declinedHistory2.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 3
        if (!Util.IsInputBlank(this.tbq3_name3.Text) ||
            !Util.IsInputBlank(this.tbq3_declinedHistory3.Text))
        {

            if (!Util.IsInputBlank(this.tbq3_name3.Text) &&
            !Util.IsInputBlank(this.tbq3_declinedHistory3.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 4
        if (!Util.IsInputBlank(this.tbq3_name4.Text) ||
            !Util.IsInputBlank(this.tbq3_declinedHistory4.Text))
        {

            if (!Util.IsInputBlank(this.tbq3_name4.Text) &&
            !Util.IsInputBlank(this.tbq3_declinedHistory4.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }


    }






    protected void vd_foresseeable_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        if (!Util.IsInputBlank(this.tbq4_name1.Text) ||
            !Util.IsInputBlank(this.tbq4_foreseeableIllness1.Text))
        {

            if (!Util.IsInputBlank(this.tbq4_name1.Text) &&
            !Util.IsInputBlank(this.tbq4_foreseeableIllness1.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 2
        if (!Util.IsInputBlank(this.tbq4_name2.Text) ||
             !Util.IsInputBlank(this.tbq4_foreseeableIllness2.Text))
        {

            if (!Util.IsInputBlank(this.tbq4_name2.Text) &&
            !Util.IsInputBlank(this.tbq4_foreseeableIllness2.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        // Check item 3
        if (!Util.IsInputBlank(this.tbq4_name3.Text) ||
            !Util.IsInputBlank(this.tbq4_foreseeableIllness3.Text))
        {

            if (!Util.IsInputBlank(this.tbq4_name1.Text) &&
            !Util.IsInputBlank(this.tbq4_foreseeableIllness1.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }


        // Check item 4
        if (!Util.IsInputBlank(this.tbq4_name4.Text) ||
             !Util.IsInputBlank(this.tbq4_foreseeableIllness4.Text))
        {

            if (!Util.IsInputBlank(this.tbq4_name4.Text) &&
            !Util.IsInputBlank(this.tbq4_foreseeableIllness4.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

    }

    protected void vd_blood_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbhbp_name1.Text, this.tbhbp_reading1.Text, this.tbhbp_date1.Text, this.tbhbp_address1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbhbp_name2.Text, this.tbhbp_reading2.Text, this.tbhbp_date2.Text, this.tbhbp_address2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbhbp_name3.Text, this.tbhbp_reading3.Text, this.tbhbp_date3.Text, this.tbhbp_address3.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbhbp_name4.Text, this.tbhbp_reading4.Text, this.tbhbp_date4.Text, this.tbhbp_address4.Text };
        checkValid(inputs4, args);

    }


    
    protected void vd_hyperlipidaemia_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbhc_name1.Text, this.tbhc_level1.Text, this.tbhc_date1.Text, this.tbhc_address1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbhc_name2.Text, this.tbhc_level2.Text, this.tbhc_date2.Text, this.tbhc_address2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbhc_name3.Text, this.tbhc_level3.Text, this.tbhc_date3.Text, this.tbhc_address3.Text};
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbhc_name4.Text, this.tbhc_level4.Text, this.tbhc_date4.Text, this.tbhc_address4.Text };
        checkValid(inputs4, args);
    }



    private void checkValid(string[] inputs, ServerValidateEventArgs args)
    {

        if (!Util.checkAllBlankInput(inputs))
        {
            if (Util.checkAllNotBlankInput(inputs))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }


    protected void cbAgree_CheckedChanged(object sender, EventArgs e)
    {
        if (cbAgree.Checked)
        {
            btnSubmit.Enabled = true;
        }
        else
        {
            btnSubmit.Enabled = false;

        }


    }

    private String getFileNamePrefix()
    {
        string userId = Session["userId"] as String;
       
        String fileName = userId + "_H_"+DateTime.Now.ToString("ddMMyyyy") +"_";


        return fileName;
    }



    private String fileUpload(FileUpload fileUpload)
    {
        string filename = "";
        if (fileUpload.HasFile)
        {
            try
            {
                filename = getFileNamePrefix() + fileUpload.FileName;
                fileUpload.SaveAs(Server.MapPath(FILE_PATH) + filename);

            }
            catch (Exception ex)
            {
                //TODO StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        return filename;
    }
}